#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    vector<int>c;
    vector<vector<int>> permuteUnique(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        c.resize(nums.size());
        dfs(nums, -1);
        return ret;
    }
    void dfs(vector<int>nums, int pos)
    {
        int n = nums.size();
        if (path.size() == n)
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < n; i++)
        {
            if (c[i] == 0)
            {
                c[i] = 1;
                if (i == 0 || (nums[i - 1] != nums[i] || nums[i - 1] == nums[i] && c[i - 1] == 1))
                {
                    path.push_back(nums[i]);
                    dfs(nums, i);
                    path.pop_back();
                }
                c[i] = 0;
            }
        }
    }
};